package com.py.credit.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.constant.ResourcePath;
import com.py.credit.dao.ReportGetDao;
import com.py.credit.service.ReportContentService;
import com.py.credit.service.ReportGetService;
import com.py.credit.utils.HtmlToPdfUtils;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

@Service
public class ReportGetServiceImpl implements ReportGetService{

	@Autowired
	private ReportGetDao reportGetDao;
	
	@Autowired
	private ReportContentService reportContentService;
	
	@Override
	public List<Map<String, Object>> selectAllReportGet(String reportType, String mainType,String recieveStatus,List<String> columns,String[] additionalColumns,String condition,Page page) {
		List<Map<String, Object>> reportGetList=new ArrayList<Map<String,Object>>();
		List<String> conditions = SQLBuilder.createConditionSql(condition);
		reportGetList=reportGetDao.selectAllReportGetByConditionForPage(reportType,mainType,recieveStatus,columns, additionalColumns, conditions, page);
		return reportGetList;
	}
	@Override
	public List<Map<String, Object>> selectReportHistoryForPage(String bgbh,String lqr, Page page) {
		List<Map<String, Object>> reportGetList=new ArrayList<Map<String,Object>>();
		reportGetList=reportGetDao.selectReportHistoryForPage(bgbh,lqr, page);
		return reportGetList;
	}
	@Transactional
	@Override
	public void updateReportshzt(String bgbh, String id, String lqr,String lqtype, String lqbz,String file) {
		reportGetDao.updateReportshzt(bgbh);
		reportGetDao.insertReportHistory(bgbh, lqr, file, lqtype, lqbz, id);
		
	}
	@Override
	public List<Map<String, Object>> selectQTReportGetByConditionForPage(String reportType,String mainType,List<String> columns,
			String[] additionalColumns, String condition, Page page) {
		List<Map<String, Object>> reportGetList=new ArrayList<Map<String,Object>>();
		List<String> conditions = SQLBuilder.createConditionSql(condition);
		reportGetList=reportGetDao.selectQTReportGetByConditionForPage(reportType,mainType,columns, additionalColumns, conditions, page);
		return reportGetList;
	}
	@Override
	public List<Map<String, Object>> selectQTReportGetHistoryByConditionForPage(
			String bgbh,
			List<String> columns,
			String[] additionalColumns, String condition, Page page) {
		List<Map<String, Object>> reportGetList=new ArrayList<Map<String,Object>>();
		List<String> conditions = SQLBuilder.createConditionSql(condition);
		reportGetList=reportGetDao.selectQTReportGetHistoryByConditionForPage(bgbh,columns, additionalColumns, conditions, page);
		return reportGetList;
	}
	@Override
	public void insertPrintHistory(String evalId, String score, String userId) {
		// TODO Auto-generated method stub
		reportGetDao.insertPrintHistory(evalId, score, userId);
	}
	@Override
	public List<Map> selectPrintHistory(String id) {
		// TODO Auto-generated method stub
		return reportGetDao.selectPrintHistory(id);
	}
	@Override
	public List<Map<String, String>> selectEnterpriseByPc(String tableName, String pcId) {
		// TODO Auto-generated method stub
		return reportGetDao.selectEnterpriseByPc(tableName, pcId);
	}
	@Override
	public int updateReportStatus(String table,String reportStatus, String id) {
		// TODO Auto-generated method stub
		return reportGetDao.updateReportStatus(table,reportStatus, id);
	}
	@Override
	public String selectTemplateUrl(String tablePc,String pcId) {
		// TODO Auto-generated method stub
		return reportGetDao.selectTemplateUrl(tablePc,pcId);
	}
	@Override
	public Map<String, String> selectPcDetail(String table, String tybm, String pcId) {
		// TODO Auto-generated method stub
		return reportGetDao.selectPcDetail(table, tybm, pcId);
	}
	@Override
	public int updateReportStatusByBgbh(String table, String reportStatus, String bgbh) {
		// TODO Auto-generated method stub
		return reportGetDao.updateReportStatusByBgbh(table, reportStatus, bgbh);
	}
	
	@Override
	public int reportExport(String bgbh,String id,String pdfForXypjPagePath,String pdfJsPath,String absolutepath,String googlePath,
			HttpServletRequest request,String userId){
		
		try {
			this.updateReportStatusByBgbh("xypj_evalInfo", "1", bgbh);
			Map m=reportContentService.getEvalInfo(bgbh);
			String ztmc=String.valueOf(m.get("ztmc"));
			String url=pdfForXypjPagePath+"?bgbh="+bgbh;
			boolean googleConvent = HtmlToPdfUtils.googleConventSync(pdfJsPath,
					url,
					absolutepath + ResourcePath.CREDIT_REPORT_DIR+bgbh,ztmc+".pdf", null, false,
					googlePath);
			if(googleConvent){
				this.updateReportStatusByBgbh("xypj_evalInfo", "2", bgbh);
				this.insertPrintHistory((String.valueOf(m.get("id"))),String.valueOf(m.get("score")),userId);
				return 0;
			}else{
				this.updateReportStatusByBgbh("xypj_evalInfo", "3", bgbh);
				ExceptionLogger.error("reportGet#selectQTHistoryAllReportGet", "生成报告失败");
				return -1;
			}
		} catch (Exception e) {
			this.updateReportStatusByBgbh("xypj_evalInfo", "3", bgbh);
			ExceptionLogger.error("reportGet#selectQTHistoryAllReportGet", "生成报告失败");
			return -1;
		}
	}
	@Override
	public void updateReportStatusBypcId(String table, String reportStatus, String pcId) {
		reportGetDao.updateReportStatusBypcId(table,reportStatus, pcId);
		
	}
	
}
